package com.effectiv.gooruda.rest.ext;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Date;

import javax.ws.rs.ext.ParamConverter;
import javax.ws.rs.ext.ParamConverterProvider;
import javax.ws.rs.ext.Provider;

import org.springframework.stereotype.Component;

import jodd.typeconverter.impl.DateConverter;

import lombok.extern.slf4j.Slf4j;
@Provider
@Component
@Slf4j
public class DateParamConverterProvider implements ParamConverterProvider, ParamConverter<Date> {
	
	private DateConverter converter = new DateConverter();
	
	public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType,
			Annotation[] annotations) {
		
		log.info("Checking date converter");
		log.info("rawType = {}" , rawType);
		log.info("genericType = {}" , genericType);
		
		if (rawType == Date.class) {
            return (ParamConverter<T>)this;
        } else {
            return null;
        }
	}

	public Date fromString(String s) {
		return converter.convert(s);
	}

	public String toString(Date s) {
		return s.toString();
	}

}
